package com.mzl.lal4cloud.lxw4rf.service.contract.auto;


import com.mzl.lal4cloud.lxw4rf.service.contract.bean.ConfigProperties;
import com.mzl.lal4cloud.lxw4rf.service.contract.bean.HostConfig;
import com.mzl.lal4cloud.lxw4rf.service.contract.config.ConfigStorage;
import com.mzl.lal4cloud.lxw4rf.service.contract.config.DefaultConfigStorage;
import com.mzl.lal4cloud.lxw4rf.service.contract.service.DefaultService;
import com.mzl.lal4cloud.lxw4rf.service.contract.service.impl.ServiceOkHttpImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author J_luo
 */
@Configuration
@EnableConfigurationProperties(ConfigProperties.class)
public class ServiceAutoConfiguration {

    @Autowired
    private ConfigProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public ConfigStorage configStorage() {
        DefaultConfigStorage configStorage = new DefaultConfigStorage();
        configStorage.setClientId(this.properties.getClientId());
        configStorage.setClientSecret(this.properties.getClientSecret());
        configStorage.setPrivateKey(this.properties.getPrivateKey());
        configStorage.setHostConfig(new HostConfig(this.properties.isUseSandboxEnv()));
        return configStorage;
    }

    @Bean
    @ConditionalOnMissingBean
    public DefaultService defaultService(ConfigStorage configStorage) {
        DefaultService defaultService = new ServiceOkHttpImpl();
        defaultService.setConfigStorage(configStorage);
        return defaultService;
    }

}
